import axios from "axios";
import Qs from "qs";
import Vue from "vue";
// axios.defaults.headers['Authorization'] = 'Bearer ' + localStorage.getItem('token')
// axios.defaults.headers.post['Content-Type'] = 'application/json';
const http = {
    //   url: 'http://doctor.yuepinyuemei.com/api/v1_1',
    //   url: "http://doctor.yuepinyuemei.com/api/v1_1", //测试
    //  url: process.env.BASE_API,
    postdata(apiurl, params) {
        return new Promise((resolve, reject) => {
            axios({
                    method: "post",
                    url: this.url + apiurl,
                    params,
                    withCredentials: true
                })
                .then(res => {
                    if (res.status == 200) {
                        resolve(res.data);
                    }
                })
                .catch(res => {
                    reject(res);
                });
        });
    },
    /**
     *  let res = await http.postdatat("/card_goods/verify");
          console.log(res);
     */
    postdatat(apiurl, params) {
        return new Promise((resolve, reject) => {
            axios
                .post(this.url + apiurl, params, {
                    "Content-Type": "application/json"
                })
                .then(res => {
                    if (res.status == 200) {
                        resolve(res.data);
                    }
                })
                .catch(res => {
                    reject(res);
                });
        });
    },
    getdata(apiurl, params) {
        return new Promise((resolve, reject) => {
            axios({
                    method: "get",
                    url: this.url + apiurl,
                    params,
                    withCredentials: true
                })
                .then(res => {
                    resolve(res);
                })
                .catch(res => {
                    reject(res);
                });
        });
    },
    /**
     *  let imgFile = e.target.files[0]; //取到上传的图片

        let formData = new FormData(); //通过formdata上传
        formData.append("dir", "app_home/custom");
        formData.append("file", imgFile);
        console.log(formData);
        let res = await posthttp.postimg("upfile5", formData);
     */
    postimg(apiurl, params) {
        return new Promise((resolve, reject) => {
            axios
                .post(this.url + apiurl, params, {
                    "Content-Type": "multipart/form-data"
                })
                .then(res => {
                    resolve(res);
                })
                .catch(res => {
                    reject(res);
                });
        });
    }
};
export default http;